package com.kantboot.system.dict.service.impl;

import com.kantboot.system.dict.domain.entity.SysDict;
import com.kantboot.system.dict.dao.repository.SysDictRepository;
import com.kantboot.system.dict.service.ISysDictService;
import com.kantboot.util.common.exception.BaseException;
import com.kantboot.util.core.param.PageParam;
import com.kantboot.util.core.result.PageResult;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class SysDictServiceImpl implements ISysDictService {

    @Resource
    private SysDictRepository repository;

    @Override
    public HashMap<String, String> getMapByGroupCode(String groupCode) {
        HashMap<String,String> result = new HashMap<>(100);
        List<SysDict> byGroupCode = repository.findByGroupCode(groupCode);
        for (SysDict item : byGroupCode) {
            result.put(item.getCode(),item.getValue());
        }

        return result;
    }

    @Override
    public PageResult getBodyData(PageParam<SysDict> pageParam) {
        Page<SysDict> bodyData = repository.getBodyData(pageParam.getData(), pageParam.getPageable());
        return PageResult.of(bodyData);
    }

    @Override
    public String getValue(String groupCode, String code) {
        SysDict byGroupCodeAndCode = repository.findByGroupCodeAndCode(groupCode, code);
        if(byGroupCodeAndCode == null){
            throw BaseException.of("noHasSysDictValueOfGroupCodeIs"+groupCode+"AndCodeIs"+code,"不存在此字典，分组编码："+groupCode+"，编码："+code);
        }
        return byGroupCodeAndCode.getValue();
    }

    @Override
    public SysDict save(SysDict sysDict) {
        return repository.save(sysDict);
    }
}
